<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/commons/global.jsp"%>
<%@ include file="/commons/tag.jsp"%>
<%@ include file="/commons/js/basejs.jsp"%>
<%@ include file="/commons/js/extjs.jsp"%>
<%@ include file="/commons/css/basecss.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<body id="mainBody" class="easyui-layout">
<input class="easyui-textbox" style="width:100px;">
<table id="treeGrid" ></table>
<div id="win" class="easyui-window" title="My Window" style="width:600px;height:400px"   
        data-options="iconCls:'icon-save',modal:true,closed:true">   
	<div id="datagrid_toolbar2">
	<form id="updateForm" method="post">   
	<input type="hidden" name="id" id="id" />
	<input type="hidden" id="ageId" name="ageId" />
  	<input type="hidden" id="facId" name="facId" />
       <table style="width:100%;height:200px;border:0px">
           <tr>
			<td>发货单编号：</td>
			<td>
			   <input type="text" id="deliveryNo" name="deliveryNo" class="easyui-textbox" data-options="required:true" />
			</td>
			<td>经销商名称：</td>
			<td>
			   <input type="text" id="ageName" name="ageName" class="easyui-textbox"
			    data-options="editable:true,required:true" />
			</td>
		  </tr>
		 <tr>
			<td>交货日期：</td>			
			<td>
			  <input type="text" id="sendDate" name="sendDate" 
			  	class="easyui-datebox" data-options="editable:false">
			</td>
			<td>发票类型：</td>
			<td>
				<input id="fac" class="easyui-combobox" name="fac" 
		    data-options="valueField:'id',textField:'label',url:'${base}/SystemOrder/selectForFactory'"/>  
			</td>
		</tr>
		<tr>
		   <td>快递费：</td>
			<td>
			   <input type="text" name="expressFee" id="expressFee" class="easyui-numberbox" data-options="min:0,precision:2"></input>  
			</td>
			<td>状态:</td>
			<td>
			<!-- 1=待审核;2=待发货;3=已发货;4=已完成 -->
				<select id="deliveryStatus" class="easyui-combobox" name="deliveryStatus">   
				    <option value="1">待审核</option>  
				    <option value="2">待发货</option> 
				    <option value="3">已发货</option> 
				    <option value="4">已完成</option>   
				</select> 
			</td>
		</tr>		
        <tr>
		<td>备注：</td>
		<td width="80%" colspan="3">
			<textarea class="textbox" id="remark" name="remark" 
			style="height:100px;width:80%;white-space: pre-wrap;overflow-y:auto" cols="40"></textarea>
		</td>
		</tr>	
       </table>
       <a id="subbtn" onclick="submit()" class="easyui-linkbutton" data-options="iconCls:'icon-save'">提交</a>  
	</form>
       <table id="table3"></table>
	
		<%-- 请选择厂商 :
		<input id="fac" class="easyui-combobox" name="fac" 
		    data-options="valueField:'label',textField:'value',url:'${base}/SystemProduct/findAllFactory'"/>  
		备注:
		<input id="keyword" class="text" name="keyword" placeholder="包括订单编号/厂商名称/经销商名称"/>  
			    <a onclick="doSearch()" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-search'">查看</a>
			    <div class="td_right">
				<!-- <a onclick=";" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'">添加</a> -->
				<a onclick="findDetail()" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-edit'">编辑</a>
		       	<a onclick="deletes()" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-del'">删除</a>
			   	</div> --%>
	</div>
</div>  
	<div class="easyui-layout" data-options="fit:true,border:false" style="overflow: hidden;width:100%">
        <div data-options="region:'center',border:false"  >
            
        </div>
    </div>
    <div data-options="region:'center'" style="overflow-x: hidden">
		<div id="tabs" class="easyui-tabs" data-options="fit:true,border:false">
			<div id="systemMenu" title="订单列表">
			<table id="table1"></table>
				<div id="datagrid_toolbar">
			 	 请选择厂商 :
				<input id="fac" class="easyui-combobox" name="fac" 
		  		  data-options="valueField:'label',textField:'value',url:'${base}/SystemProduct/findAllFactory'"/>  
				关键字:
				<input id="keyword" class="text" name="keyword" placeholder="包括订单编号/经销商名称"/>  
			        <a onclick="doSearch()" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-search'">查看</a>
			        <div class="td_right">
						<!-- <a onclick=";" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'">添加</a> -->
						<a onclick="findDetail()" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-edit'">编辑</a>
				       	<a onclick="deletes()" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-del'">删除</a>
			       	</div>
				</div>
			</div>
		</div>
	</div>
</div>
	
<script type="text/javascript">
	$(function(){
		$('#table1').datagrid({
			url: '${base}/SystemOrderDelivery/findBySenior',
			fit: true,
			border: false,
			pagination: true,// 分页
			pagePosition: 'bottom',
			idField: 'id',
			pageSize: 30,
			pageList: [10,20,30,50],
			checkOnSelect: true,
			selectOnCheck: true,
			singleSelect: false,
			nowrap: true, 
			rownumbers: true, //显示行号
			showfooter: true, //显示行尾
			pageNumber: 1,
			fitColumns:true,
			sortName: 'updateTime',	//按字段排序
			sortOrder: 'desc',		//倒序
			toolbar:'#datagrid_toolbar',
			columns : [[
			 {field : 'id', title :'id', width : 50, hidden: true},
			 {field : 'deliveryNo', title:'发货单编号', width : '20%', align : 'center'},
			 {field : 'facName', title:'厂商名称', width : '20%', align : 'center'},
			 {field : 'ageName', title:'经销商名称', width : '15%', align : 'center'},
			 {field : 'amountTotal', title:'订单金额', width : '15%', align : 'center'},
			 {field : 'deliveryStatus', title:'发货单状态', width : '15%', align : 'center',sortable:true,
				 //发货单状态：1=待审核;2=待发货;3=已发货;4=已完成
				 formatter:function(value,row,index){
			 		 if (1 == value) return '待审核';
					 else if (2 == value) return '待发货';
					 else if (3 == value) return '已发货';
					 else if (4 == value) return '已完成';
					 else return '';
			 	}}]],
	   });
	});
		function deletes(){
			 var row = $('#table1').datagrid('getSelections');
			var ids = "";
				for(var i in row){
					ids += ","+row[i].id;
				}
				console.info(ids);
			   $.ajax({  
                type : "POST",  
                url : "${base}/SystemOrderDelivery/deleteDelOrder",
                data : {
                	ids: ids
                },
                success : function(result) {
                	if(result == 0){
                    	swal("", "删除失败,包含默认商品","error");
                	} else {
                		/* swal("", "删除成功","success"); */
                	}
                	$('#table1').datagrid('load',{
						condition: $('#keyword').val(),
						facId: $('#fac').combobox('getValue')
					});
                }  
            });      
		}
		function doSearch(){
			$('#table1').datagrid('load',{
				keyword: $('#keyword').val(),
				facId: $('#fac').combobox('getValue')
			});
		}
		
		function findDetail(){
			 var row = $('#table1').datagrid('getSelections');
			 if(row.length != 1){
				 swal("", "操作失败,请选择一个商品","error");
				 return;
			 }
			 $.ajax({  
	                type : "POST",  
	                url : "${base}/SystemOrderDelivery/findDeliveryDatailsById",
	                data : {
	                	facId: row[0].facId,
						id: row[0].id
	                },
	                success : function(result) {
	                	var date = JSON.parse(result); 
	                	console.info(date);
	                	$('#updateForm').form('load',date);
	                }  
	            });
			 $('#table3').datagrid({
				url: '${base}/SystemOrder/orderDetailList',
         	    queryParams: {
         	    	facId: row[0].facId,
					id: row[0].id
         		},
         		iconCls: 'icon-edit',
				singleSelect: true,
				onClickCell: 'onClickCell()',
         		columns : [[
        					 {field : 'id', title :'id', width : '4%', hidden: true},
        					 {field : 'productName', title:'商品名字', width : '15%', align : 'center'},
        					 {field : 'productNumber', title:'商品编号', width : '15%', align : 'center'},
        					 {field : 'productSpec', title:'商品规格', width : '15%', align : 'center'},
        					 {field : 'unitName', title:'单位', width : '12%', align : 'center'},
        					 {field : 'promotionPrice', title:'商品价格', width : '12%', align : 'center'},
        					 {field : 'amount', title:'数量', width : '12%', align : 'center' ,editor:'numberbox'},
        					 {field : 'basicPrice', title:'小计', width : '12%', align : 'center'},
        					 {field :'action', title:'操作', width:'12%', align:'center',
        							formatter:function(value,row,index){
        								var d = '<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:\'icon-del\',plain:true" onclick="deleteLine(this)">删除</a>';
        								var e = '<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:\'icon-del\',plain:true" onclick="saveLine(this)">保存</a>';
        								return d+e;
        							}
        						} ]]
         	
         	}).datagrid('enableCellEditing').datagrid('disableCellSelecting');
			$('#win').window('open');  // open a window    
		}
		 function submit(){
			$('#updateForm').form({    
			    url:'${base}/SystemOrder/modify',    
			    onSubmit: function(){    
			    },    
			    success:function(data){ 
			    	
			    }    
			});      
			$('#updateForm').submit();
	    	$('#win').window('close');  
		}
		
		function saveLine(rec){
			var a=getRowIndex(rec);
			var row = $('#table3').datagrid("getRows")[a];
			console.info(row);
			$.ajax({  
                type : "POST",  
                url : "${base}/SystemOrder/modifyDetail",
                data : {
                	date: JSON.stringify(row),
                	amount:row.amount
                },
                success : function(result) {
                	 $('#table3').datagrid('load'); 
                }  
            });
		}
		
		function deleteLine(rec){
			var a=getRowIndex(rec);
			var line = $('#table3').datagrid("getRows")[a];
			console.info(line);
			 $.ajax({  
                type : "POST",  
                url : "${base}/SystemOrder/deleteDetail",
                data : {
                	id:line.id
                },
                success : function(result) {
                	if(result == 0){
                    	swal("", "删除失败,包含默认商品","error");
                	}
                	  $('#table3').datagrid('load'); 
                }  
            });  
		} 
		//======================================================================
		
		$.extend($.fn.datagrid.methods, {
			editCell: function(jq,param){
				return jq.each(function(){
					var opts = $(this).datagrid('options');
					var fields = $(this).datagrid('getColumnFields',true).concat($(this).datagrid('getColumnFields'));
					for(var i=0; i<fields.length; i++){
						var col = $(this).datagrid('getColumnOption', fields[i]);
						col.editor1 = col.editor;
						if (fields[i] != param.field){
							col.editor = null;
						}
					}
					$(this).datagrid('beginEdit', param.index);
					for(var i=0; i<fields.length; i++){
						var col = $(this).datagrid('getColumnOption', fields[i]);
						col.editor = col.editor1;
					}
				});
			}
		});
		
		var editIndex = undefined;
		function endEditing(){
			if (editIndex == undefined){return true}
			if ($('#table3').datagrid('validateRow', editIndex)){
				$('#table3').datagrid('endEdit', editIndex);
				editIndex = undefined;
				return true;
			} else {
				return false;
			}
		}
		function onClickCell(index, field){
			if (endEditing()){
				$('#table3').datagrid('selectRow', index)
						.datagrid('editCell', {index:index,field:field});
				editIndex = index;
			}
		}
	</script>
	<script type="text/javascript" src="${jsPath }/extends/datagrid-cellediting.js" charset="utf-8"></script>
</body>
</html>